<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\paginator\driver\Bootstrap;
use think\Url;

if (!Session::has('DSL','admin')){
    die('非法登录');
}
class Questions extends Controller
{
    public function index()
    {
        $keyword = input('get.keyword');
        if (trim($keyword) != ''){
            $question_list = db('questions')->where('question_name','like','%'.$keyword.'%')->field(['id','question_name','release'])->paginate(10);
        }else {
            $question_list = db('questions')->field(['id','question_name','release'])->paginate(10);
        }
        $page = $question_list->render();
        $question_list = $question_list->toArray();
        $question_list = $question_list['data'];
        foreach ($question_list as $qk => &$v){
        	if ($v['release'] == '1'){
        		$v['release'] = 'on';
        	}else {
        		$v['release'] = 'off';
        	}
        
        }
        $this->assign('questions_list',$question_list);
        $this->assign('page',$page);
        return $this->fetch('index');
    }
    
    public function update_release()
    {
    	$result = array('err' => '1','content' => '');
    	$qusetions_id = input('get.questions_id');
    	$questions_list = array();
    	$questions_list['release'] = input('get.release');
    	$update_release = db('questions')->where('id',$qusetions_id)->update($questions_list);
    	if ($update_release == false){
    		$result['content'] = '发布状态更新错误！';
    		return $result;
    	}
    	$result['err'] = '0';
    	$result['content'] = '发布状态更新成功！';
    	return $result;
    }
    
    public function save_questions()
    {
        $questions_id = input('get.questions_id');
        if ($questions_id != null){
            $questions_list = db('questions')->where('id',$questions_id)->find();
            $questions_name = $questions_list['question_name'];
            $questions_list = $questions_list['questions'];
            $questions_list = json_decode($questions_list,true);
            $curpage = input('page') ? input('page') : 1;
            $data = $questions_list;
            $data_to = array_chunk($data, 8);
            $questions_list = array();
            if ($data_to){
            	$questions_list = $data_to[$curpage-1];
            }else {
            	$questions_list = null;           
            }
            $questions_list = Bootstrap::make($questions_list,8,$curpage,count($data),false,[
                'var_page' =>'page',
                'path' => Url::build('save_questions'),
                'query' => request()->param(),
                'fragment' => ''
            ]);
            $questions_list->appends($_GET);  
            $page = $questions_list->render();
            $this->assign('questions_id',$questions_id);
            $this->assign('questions_name',$questions_name);
            $this->assign('question_list',$questions_list);
            $this->assign('page',$page);
            $this->assign('action','edit');
        }else {
            $this->assign('action','add');
        }
        return $this->fetch('save_questions');
    }
    
    public function save()
    {
    	$result = array('err' => '1','content' => '');
        $questions_id = input('post.questions_id');
        $questions_name = input('post.questions_name');
        $questions_arr = input('post.questions');
        $questions = model('Questions');
        $save_questions = $questions->saveQuestions($questions_id,$questions_name,$questions_arr);
        if ($save_questions == false){
        	$result['content'] = '保存失败！';
        	return $result;
        }
        $result['err'] = '0';
        $result['content'] = '保存成功！';
        return $result;
    }
    
    public function del_questions()
    {
    	$result = array('err' => '1','content' => '');
    	$questions_id = input('post.id');
    	$del_questions = db('questions')->where('id',$questions_id)->delete();
    	if ($del_questions == false){
    		$result['content'] = '删除题目失败！';
    		return $result;
    	}
    	$result['err'] = '0';
    	$result['content'] = '删除题目成功！';
    	return $result;
    }
    
    public function questions_analyse()
    {
        $questions_id = input('get.questions_id');
        $questions_model = model('Questions');
        $questions_info = db('questions')->where('id',$questions_id)->field(['id','question_name'])->find();
        $warehouse_avg = $questions_model->getWarehouseAvgScore($questions_id);
        $warehouse_avg = json_encode($warehouse_avg);
        $total_false = $questions_model->getFalseNum($questions_id,'all');
        $each_warehouse_false = $questions_model->getWarehouseFalseNum($questions_id);
        $total_false = json_encode($total_false);
        $each_warehouse_false_json = json_encode($each_warehouse_false);
        $this->assign('questions_info',$questions_info);
        $this->assign('warehouse_avg',$warehouse_avg);
        $this->assign('total_false',$total_false);
        $this->assign('each_warehouse_false',$each_warehouse_false);
        $this->assign('each_warehouse_false_json',$each_warehouse_false_json);
//         var_dump($total_false);
//         var_dump($each_warehouse_false);
        return $this->fetch('questions_analyse');
    }
}